<template>
  <AMISRenderer :schema="schema" />
</template>

<script setup>
import AMISRenderer from '@/components/AMISRenderer.vue'
import createAddSchema from './add'

const schema = {
    type: 'page',
    initApi: '/upms/user/region/province',
    body: {
      type: 'crud',
      api: {
        url: '/upms/admin/org/nhsa/list',
        adaptor: function (payload, response, api, context) {
          //因为后端存的省市县值和下拉框不匹配所以需要处理为null（省-省-省、省-市-市）
          if(payload.data && payload.data.items){
            for(let i=0;i<payload.data.items.length;i++){
              if(payload.data.items[i].regionLevel == 1){
                payload.data.items[i].cityCode = null;
                payload.data.items[i].areaCode = null;
              }else if(payload.data.items[i].regionLevel == 2){
                payload.data.items[i].areaCode = null;
              }
            }
          }
          
          return {
              ...payload
          };
        }
      },
      syncLocation: false,
      autoFillHeight: true,
      footerToolbar: [
        'statistics',
        'switch-per-page',
        'pagination'
      ],
      autoGenerateFilter: {
        showBtnToolbar: false,
        columnsNum: 4
      },
      headerToolbar:[
        {
          type: 'button',
          label: '导入',
          align: 'right',
          icon: 'fa fa-file-excel-o',
          actionType: 'dialog',
          dialog:{
            title: '导入医保局',
            closeOnEsc: true,
            showErrorMsg: false,
            body:{
              type: 'form',
              api: '/upms/admin/org/nhsa/import',
              messages:{
                  validateFailed:''
              },
              body:[
                {
                  type: 'input-file',
                  name: 'fileUrl',
                  label: '上传文件',
                  maxSize: 20971520,
                  accept: '.xls,.xlsx',
                  required: true,
                  validationErrors: {
                    isRequired: '请上传文件'
                  },
                  receiver: '/base/file/upload',
                  autoFill: {
                    fileId: '${fileId}',
                    fileUrl: '${url}'
                  }
                },
                {
                  type: 'select',
                  name: 'roleIds',
                  label: '选择角色',
                  searchable: true,
                  multiple: true,
                  source: '/upms/admin/role/select',
                  clearable: true
                },
                {
                  type: 'control',
                  label: '模板下载',
                  body:{
                      type: 'action',
                      level: 'link',
                      actionType: 'url',
                      label: '医保局导入模板',
                      url: 'https://oss.aliyun.com/template/upms/%E6%A8%A1%E6%9D%BF-%E5%8C%BB%E4%BF%9D%E5%B1%80%E5%AF%BC%E5%85%A5.xlsx',
                  },
                },
                {
                  type: 'hidden',
                  name: 'fileId'
                },
                {
                  type: 'hidden',
                  name: 'fileUrl'
                },
              ]
            }
          }
        },
        {
          type: 'button',
          actionType: 'dialog',
          align: 'right',
          label: '新增',
          icon: 'fa fa-plus pull-left',
          primary: true,
          dialog: {
            title: '新增医保局',
            closeOnEsc: true,
            showErrorMsg: false,
            size: 'md',
            body: createAddSchema('/upms/admin/org/nhsa/add', false)
          }
        }
      ],
      columns: [
        {
          name: 'orgName',
          label: '医保局名称',
          searchable: {
            type: 'input-text',
            name: 'orgName',
            label: '医保局名称',
            clearable: true
          }
        },
        {
          name: 'parentName',
          label: '上级医保局'
        },
        {
          name: 'orgCode',
          label: '编码'
        },
        {
          name: 'provinceName',
          label: '省',
          hiddenOn: '1',
          searchable: {
            type: 'select',
            name: 'provinceCode',
            label: '省',
            source: '${provinceList}',
            valueField: 'regionCode',
            labelField: 'regionName',
            clearable: true,
          }
        },
        {
          name: 'cityName',
          label: '市',
          hiddenOn: '1',
          searchable: {
            type: 'select',
            name: 'cityCode',
            label: '市',
            source: {
              url: '/upms/user/region/children?parentId=${provinceCode}',
              sendOn: '${provinceCode}',
            },
            valueField: 'regionCode',
            labelField: 'regionName',
            clearable: true,
          }
        },
        {
          name: 'areaName',
          label: '县',
          hiddenOn: '1',
          searchable: {
            type: 'select',
            name: 'areaCode',
            label: '县',
            source: {
              url: '/upms/user/region/children?parentId=${cityCode}',
              sendOn: '${cityCode}',
            },
            valueField: 'regionCode',
            labelField: 'regionName',
            clearable: true,
          }
        },
        {
          name: 'regionFullName',
          label: '所属片区'
        },
        {
          name: 'mainUserName',
          label: '主账号'
        },
        {
          type: 'operation',
          label: '操作',
          fixed: 'right',
          buttons: [
            {
              label: '修改',
              type: 'button',
              actionType: 'dialog',
              dialog:{
                title: '修改医保局',
                closeOnEsc: true,
                showErrorMsg: false,
                body: createAddSchema('/upms/admin/org/nhsa/update', true),
                size: 'md'
              }
            },
            {
              label: '删除',
              type: 'button',
              level: 'danger',
              actionType:'ajax',
              confirmText: '确认要删除该记录？',
              api: {
                url: '/upms/admin/org/nhsa/delete?id=${id}',
                method: 'post',
                data:{}
              },
            }
          ]
        }
      ]
    }
  }

</script>
